<?php 
namespace Admin\Controller;

use Common\Controller\AdminBaseController;

class PositionController extends AdminBaseController{
	public function index(){
        $position = M('position');
        $list = $position->field('id,title,width,height,type,num,description,status')->order('id desc')->select();
        $this->assign('list',$list);
        $this->display();
    }
	public function add(){
        $pid = I('get.pid/d',0);
        $posInfo = M('position')->field('id,title,width,height,type,num,description,status')->select();
        $this->assign('posInfo',$posInfo);
        $this->display();
    }
    public function edit(){
        if(IS_GET){
            $id = I('get.id/d',0);
            if(!$id){
                $this->error('非法操作！');
            }
            $posInfo = M('position')->field('id,title,width,height,type,num,description,status')->where(['id'=>$id])->find();
            $this->assign('posInfo',$posInfo);
            $this->display('position/add');
        }
    }
    public function save(){
        if(IS_POST || IS_AJAX){
            $id = I('post.id/d',0);
            $title = I('post.title/s','');
            if(!$title){
                $this->ajaxReturn(['status'=>1,'msg'=>'版位名称不能为空！']);
            }
            $type = I('post.type/s',0);
            if(!$type){
                $this->ajaxReturn(['status'=>1,'msg'=>'版位类型不能为空！']);
            }
            $description = I('post.description/s','');
            $width = I('post.width/d',0);
            $height = I('post.height/d',0);
            $num = I('post.num/d',0);
            //$num = M('Ad')->where(['position'=>$position])->count();
            $position = M('position');
            $status = I('post.status/d',1);
            $posNum = $position->where(['title'=>$title,'type'=>$type])->count();
            if($posNum > 0){
                $this->ajaxReturn(['status'=>1,'msg'=>'版位已存在！']);
            }
            $data = [
                'id' => $id,
                'title'=>$title,
                'type'=>$type,
                'width'=>$width,
                'height'=>$height,
                'num'=>$num,
                'description'=>$description,
                'status'=>$status
            ];
            $position = M('position');
            $isSQL = $position->add($data,[],true);
            if(!$isSQL){
                $this->ajaxReturn(['status'=>1,'msg'=>'失败！']);
            }
            $this->ajaxReturn(['status'=>0,'msg'=>'成功！']);
        }
    }
    public function changeState(){
        if(IS_POST){
            $id = I('post.id/d',0);
            if(empty($id)){
                $this->ajaxReturn(['status' => 1 , 'msg' => '非法操作！']);
            }
            $posInfo = M('position')->field('status')->where(['id'=>$id])->find();
            if(empty($posInfo['status'])){
                M('position')->where(['id' => $id])->setField('status',1);
                $this->ajaxReturn(['status' => 0 , 'msg' => '已开启' , 'data' => 1]);
            }else{
                M('position')->where(['id' => $id])->setField('status',0);
                $this->ajaxReturn(['status' => 0 , 'msg' => '已禁用' , 'data' => 0]);
            }
        }else{
            $this->ajaxReturn(['status' => 1 , 'msg' => '非法操作！']);
        }
    }
    public function del(){
        if(IS_POST){
            $id = I('post.id/d',0);
            if(empty($id)){
                $this->ajaxReturn(['status' => 1 , 'msg' => '非法操作!']);
            }
            $isDel = M('position')->where(['id' => $id])->delete();
            if(!$isDel){
                $this->ajaxReturn(['status' => 1 , 'msg' => '删除失败！']);
            }
            $this->ajaxReturn(['status' => 0 , 'msg' => '删除成功！']);
        }else{
            $this->ajaxReturn(['status' => 1 , 'msg' => '非法操作!']);
        }
    }
}